package com.it.schoolbookshop_back.mapper;

import com.it.schoolbookshop_back.entities.po.Rating;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 评分Mapper接口
 */
@Mapper
public interface RatingMapper {
    
    /**
     * 插入评分记录
     * @param rating 评分信息
     * @return 影响行数
     */
    @Insert("INSERT INTO ratings (transaction_id, rater_id, rated_id, score, comment) " +
            "VALUES (#{transactionId}, #{raterId}, #{ratedId}, #{score}, #{comment})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Rating rating);
    
    /**
     * 根据交易ID和评分人ID查询评分记录
     * @param transactionId 交易ID
     * @param raterId 评分人ID
     * @return 评分记录
     */
    @Select("SELECT * FROM ratings WHERE transaction_id = #{transactionId} AND rater_id = #{raterId}")
    Rating selectByTransactionAndRater(@Param("transactionId") Integer transactionId, 
                                     @Param("raterId") Integer raterId);
    
    /**
     * 根据被评分人ID查询评分记录
     * @param ratedId 被评分人ID
     * @return 评分记录列表
     */
    @Select("SELECT * FROM ratings WHERE rated_id = #{ratedId} ORDER BY created_at DESC")
    List<Rating> selectByRatedId(@Param("ratedId") Integer ratedId);
    
    /**
     * 根据用户ID查询该用户作为评分人的所有评分记录
     * @param userId 用户ID
     * @return 评分记录列表
     */
    @Select("SELECT * FROM ratings WHERE rater_id = #{userId} ORDER BY created_at DESC")
    List<Rating> selectByUserId(@Param("userId") Integer userId);
} 